package top.fzqblog.service;

import com.google.gson.Gson;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import top.fzqblog.model.MusicList;
import top.fzqblog.model.Song;
import top.fzqblog.music.util.XmlUtil;


/**
 * Created by 抽离 on 2017/10/13 0013.
 */

public class SongService {

    public void getSong(Subscriber<Song> subscriber, String rid){
        ApiFactory
                .getSongApiSingleton()
                .getSong(rid)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .map(new Func1<String, Song>() {
                    @Override
                    public Song call(String s) {
                        return (Song) XmlUtil.getXMLObject(s, Song.class);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getMusicList(Subscriber<MusicList> subscriber, String key){
        ApiFactory
                .getMusicListApiSingleton()
                .getList(6, 1, 500, "music", key)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .map(new Func1<String, MusicList>() {
                    @Override
                    public MusicList call(String s) {
                        return new Gson().fromJson(s, MusicList.class);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
}
